package com.hhh.petadopt.controller;


import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.ServletContext;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.multipart.MultipartFile;

import com.hhh.petadopt.pojo.Pet;
import com.hhh.petadopt.pojo.PetPage;
import com.hhh.petadopt.pojo.PetVo;
import com.hhh.petadopt.service.AdminPetService;
import com.hhh.petadopt.utils.JsonUtils;
import com.hhh.petadopt.utils.Page;

/**
 * 宠物管理后台
 * @author 杜镇声
 *
 */
@Controller
public class AdminPetcontroller implements ServletContextAware{
	
	@Autowired
	private AdminPetService adminpetservice;
	
	@Override
	public void setServletContext(ServletContext arg) {
		
	}
	
	@RequestMapping(value="/admin/pet.html")
	public String showpet(PetPage vo,Model model) {
		Page<Pet> page = adminpetservice.selectPage(vo);
		model.addAttribute("page", page);
		model.addAttribute("total", page.getTotal());
		return "admin/pet";
	}
	//分页显示页面
	@RequestMapping(value="/admin/petlist.html")
	public String selectpages(PetVo vo,Model model) {
		//执行查询
		Page<Pet> page = adminpetservice.selectPage(vo);
		/*
		 * 将搜索信息回显
		 */
		//编号
		model.addAttribute("num", vo.getNumm());
		//年龄
		model.addAttribute("age", vo.getAgee());
		//性别
		model.addAttribute("sexg", vo.getSexx());
		//状态
		model.addAttribute("item", vo.getSts());
		
		//将分页信息存储到域中
		model.addAttribute("total", page.getTotal());
		model.addAttribute("page", page);
		return "admin/pet";
	}
	//文件上传
	@RequestMapping(value="/admin/uploadPic.html")
	@ResponseBody
	public String uploadPetPic(MultipartFile imgs) throws IllegalStateException, IOException{
		//设置图片名称
		String pname = UUID.randomUUID().toString().replace("-", "");
		//获取原本图片名称
		String originalFilename = imgs.getOriginalFilename();
		//获取图片扩展名
		String extName = FilenameUtils.getExtension(originalFilename);
				
		//将文件存到upload文件夹下
		imgs.transferTo(new File("D:/upload/image/"+pname+"."+extName));
		
		//将图片的名称进行拼接
		String pic = "/pic/"+pname+"."+extName;
		
		String objectToJson = JsonUtils.objectToJson(pic);
		
		//返回拼接好的完整的图片路径
		return objectToJson;
	}
	
	//添加宠物
	@RequestMapping(value="/admin/Addpet.html")
	@ResponseBody
	public String addPet(Pet pet) {
		/*pet.setPimage(pimage);*/
		//执行操作
		adminpetservice.addPett(pet);
		return "ok";
	}
	
	//编辑信息回显
	@RequestMapping(value="/adminpet/selectPet.html")
	@ResponseBody
	public String selectPet(Integer id) {
		//执行查询
		Pet pet = adminpetservice.selectPett(id);
		String objectToJson = JsonUtils.objectToJson(pet);
		return objectToJson;
	}
	
	//编辑页面图片上传
	@RequestMapping(value="/admin/uploadPetPic.html")
	@ResponseBody
	public String uploadPetPic1(MultipartFile imgss) throws IllegalStateException, IOException{
		//设置图片名称
		String pname = UUID.randomUUID().toString().replace("-", "");
		//获取原本图片名称
		String originalFilename = imgss.getOriginalFilename();
		//获取图片扩展名
		String extName = FilenameUtils.getExtension(originalFilename);
				
		//将文件存到upload文件夹下
		imgss.transferTo(new File("D:/upload/image/"+pname+"."+extName));
		
		//将图片的名称进行拼接
		String pic = "/pic/"+pname+"."+extName;
		
		String objectToJson = JsonUtils.objectToJson(pic);
		
		//返回拼接好的完整的图片路径
		return objectToJson;
	}
	@RequestMapping(value="/adminpet/updatePet.html")
	@ResponseBody
	public String updatePet(Pet pet) {
		//执行操作
		adminpetservice.updatePett(pet);
		return "ok";
	}
	
	//根据id删除动物信息
	@RequestMapping(value="/adminPet/deletepet.html")
	@ResponseBody
	public String deletePet(Integer id) {
		adminpetservice.deletePetById(id);
		return "ok";
	}
	//全选或全部选删除
	@RequestMapping(value="/adminPet/deletePetss.html")
	@ResponseBody
	public String delPet(Integer[] ids) {
		//执行操作
		adminpetservice.deletePets(ids);
		return "ok";
	}
	

}
